package com.zy.controller;

import com.zy.entity.User;
import com.zy.service.FriendService;
import com.zy.service.UserService;
import com.zy.utils.Pages;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author 张宇
 * @date 2021/12/6 22:32
 */
@Controller
@ResponseBody
@RequestMapping("/friend")
public class FriendController
{

    @Autowired
    private FriendService friendService;

    @Autowired
    private UserService userService;

    /**
     * 通过好友名称和用户id查看是否已经添加
     * @param friendName 好友名称
     * @param userId 用户id
     */
    @PostMapping("/addFriends")
    public Map<String,Object> addFriends(String friendName, int userId)
    {
        Map<String,Object> map = new HashMap<>();
        User user = userService.findUserByUserName(friendName);
        if(user == null)
        {
            map.put("yn","no");
            map.put("msg","用户不存在!");
            return map;
        }
        else if(user.getId() == userId)
        {
            map.put("yn","no");
            map.put("msg","你不能添加自己为好友!");
            return map;
        }
        int applyCount = friendService.findFriend(friendName,userId,0);
        if(applyCount > 0)
        {
            map.put("yn","no");
            map.put("msg","已经向好友发送过申请了!");
            return map;
        }
        int friendCount = friendService.findFriend(friendName,userId,1);
        if(friendCount > 0)
        {
            map.put("yn","no");
            map.put("msg","你们已经是好友了!");
            return map;
        }
        if(applyCount == 0 && friendCount == 0)
        {
            friendService.addFriend(friendName,userId);
            map.put("yn","yes");
            map.put("msg","已经发送申请!");
            return map;
        }
        map.put("yn","no");
        map.put("msg","发生错误!");
        return map;
    }

    /**
     * 查询好友列表
     * @param pageNo 当前页码
     * @param pageSize 每页条数
     * @return
     */
    @GetMapping("/friendsList")
    public Map<String,Object> friendsList(int pageNo,int pageSize,int userId,String friendName)
    {
        List<User> list = friendService.findFriendListByUserId(userId,friendName);//好友集合
        Map<String,Object> map = Pages.pageUtil(pageNo,pageSize,list);
        return map;
    }

    /**
     * 查询申请列表
     * @param pageNo 当前页码
     * @param pageSize 每页条数
     * @return
     */
    @GetMapping("/applysList")
    public Map<String,Object> applysList(int pageNo,int pageSize,int userId)
    {
        List<User> list = friendService.findApplysListByUserId(userId);//好友集合
        Map<String,Object> map = Pages.pageUtil(pageNo,pageSize,list);//分页
        return map;
    }

    /**
     * 同意申请
     * @param userId
     * @param friendId
     * @return
     */
    @GetMapping("/agreeApply")
    public Map<String,Object> agreeApply(int userId,int friendId)
    {
        friendService.agreeFriendStatus(userId,friendId,1);//修改好友申请状态把申请状态变为好友状态
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","添加成功!");
        return map;
    }

    /**
     * 拒绝申请
     * @param userId
     * @param friendId
     * @return
     */
    @GetMapping("/refuseApply")
    public Map<String,Object> refuseApply(int userId,int friendId)
    {
        friendService.deleteFriendApply(userId,friendId);//删除好友申请
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","已拒绝!");
        return map;
    }

}
